package com.leyou.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leyou.common.pojo.R;
import com.leyou.order.data.entity.OrderMail;
import com.leyou.order.service.OrderMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  收货地址
 * </p>
 *
 * @author xhh
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/leyou.order/order-mail")
public class OrderMailController {
    @Autowired
    private OrderMailService orderMailService;
    /**
    * todo 默认地址
    * @param userId
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/18 14:41
    * @deprecated
    */
    @RequestMapping(value = "getMailDefault",method = RequestMethod.POST)
    public R getMailDefault(@RequestParam("userId")Integer userId){
        LambdaQueryWrapper<OrderMail> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OrderMail::getUserId,userId);
        lambdaQueryWrapper.eq(OrderMail::getState,1);
        OrderMail one = orderMailService.getOne(lambdaQueryWrapper);
        return R.ok().data("one",one);
    }
    /**
    * todo 根据id获取地址
    * @param mailId
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/20 8:45
    * @deprecated 
    */
    @RequestMapping(value = "getMailById",method = RequestMethod.POST)
    public R getMailById(@RequestParam("mailId")Integer mailId){
        OrderMail byId = orderMailService.getById(mailId);
        return R.ok().data("one",byId);
    }
    /**
     * todo 地址列表
     * @param userId
     * @return com.leyou.common.pojo.R
     * @author xhh
     * @data 2022/7/18 14:41
     * @deprecated
     */
    @RequestMapping(value = "mailList",method = RequestMethod.POST)
    public R mailList(@RequestParam("userId")Integer userId){
        LambdaQueryWrapper<OrderMail> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OrderMail::getUserId,userId);
        List<OrderMail> list = orderMailService.list(lambdaQueryWrapper);
        return R.ok().data("list",list);
    }
    /**
    * todo 添加地址
    * @param orderMail
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/18 14:51
    * @deprecated
    */
    @RequestMapping(value = "mailInsert",method = RequestMethod.POST)
    public R mailInsert(OrderMail orderMail){
        boolean save = orderMailService.save(orderMail);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
    * todo 修改地址
    * @param orderMail
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/18 14:51
    * @deprecated
    */
    @RequestMapping(value = "mailModified",method = RequestMethod.POST)
    public R mailModified(OrderMail orderMail){
        boolean save = orderMailService.updateById(orderMail);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
    * todo 删除地址
    * @param mailId
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/18 14:52
    * @deprecated
    */
    @RequestMapping(value = "mailRemove",method = RequestMethod.POST)
    public R mailRemove(@RequestParam("id") Integer mailId){
        boolean save = orderMailService.removeById(mailId);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
    * todo 设置默认地址
    * @param mailId
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/18 14:54
    * @deprecated 
    */
    @RequestMapping(value = "setMailDefault",method = RequestMethod.POST)
    public R setMailDefault(@RequestParam("mailId") Integer mailId,@RequestParam("userId")Integer userId){
        LambdaQueryWrapper<OrderMail> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OrderMail::getUserId,userId);
        boolean a=false;
        boolean b=false;
        List<OrderMail> list = orderMailService.list(lambdaQueryWrapper);
        for (OrderMail orderMail : list) {
            if(orderMail.getId().equals(mailId)){
                orderMail.setState(1);
                a=orderMailService.updateById(orderMail);
            }
            else if(orderMail.getState()==1){
                orderMail.setState(2);
                b=orderMailService.updateById(orderMail);
            }
        }
        if(a && b){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

